package com.heima.behavior.service.impl;


import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApArticleLikesService;
import com.heima.behavior.service.LikeService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class LikeServiceImpl implements LikeService {


    private final CacheService cacheService;

    private final ApArticleLikesService apArticleLikesService;

    /**
     * 点赞文章 存redis
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeApArticle(LikesBehaviorDto dto) {
        // TODO:未做持久化
        // 1、检查用户
        Integer userId = AppThreadLocalUtil.getUser().getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 2、检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3、构建redis key
        String key;
        if (dto.getType() == 0) {
            return apArticleLikesService.likeAction(dto, userId);
           // key = BehaviorConstants.ARTICLE_LIKE + "_" + dto.getArticleId();
        } else if (dto.getType() == 1) {
            key = BehaviorConstants.DYNAMIC_LIKE + "_" + dto.getArticleId();
        } else {
            key = BehaviorConstants.COMMENT_LIKE + "_" + dto.getArticleId();
        }
        // 4、点赞 or 取消点赞
/*        if (dto.getOperation() == 0) {
            if (cacheService.hGet(key, userId) != null) {
               return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已点赞");
            }
            cacheService.hPut(key,userId, JSON.toJSONString(dto));
        } else {
            cacheService.hDelete(key,userId);
        }*/
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
